//201902104014付饶
package cn.edu.sdjzu.xg.bysj.dao;

import cn.edu.sdjzu.xg.bysj.domain.School;
import cn.edu.sdjzu.xg.bysj.service.SchoolService;
import test.BysjException;
import util.Condition;
import util.JdbcHelper;
import util.Pagination;

import java.sql.*;
import java.util.Collection;
import java.util.List;
import java.util.TreeSet;

public final class SchoolDao {
    private static SchoolDao schoolDao = new SchoolDao();


    private SchoolDao() {
    }


    public static SchoolDao getInstance() {
        return schoolDao;
    }
    public static Collection<School> findAll(List conditionList, Pagination pagination, Connection connection) throws SQLException {
        //创建集合类对象，用来保存所有记录代表的School对象
        Collection<School> schools = new TreeSet<School>();
        //准备预编译的语句
        int totalNum = CommonDao.getInstance().count(connection,"school");
        //创建查询的主语句
        StringBuilder sql_select = new StringBuilder("SELECT * FROM school ");
        //如果有条件，则生成条件子句，并附加到主语句后面
        if(conditionList!=null){
            String whereClause = Condition.toWhereClause(conditionList);
            sql_select.append(whereClause);
        }
        if(pagination!=null){
            String LimitClause = pagination.toLimitClause(totalNum);
            sql_select.append(LimitClause);
        }
        //执行预编译语句，结果集放在resultSet中
        PreparedStatement pstmt = connection.prepareStatement(sql_select.toString());
        ResultSet resultSet = pstmt.executeQuery();
        //遍历resultSet
        while(resultSet.next()){
            schools.add(new School(resultSet.getInt("id"),
                    resultSet.getString("description"),
                    resultSet.getString("no"),
                    resultSet.getString("remarks")));
        }
        return schools;
    }

    public static School find(int id,Connection connection) throws SQLException {

        //创建sql语句
        String find_sql = "select * from School where id = ?";
        //在该连接上创建预编译语句对象
        PreparedStatement pstmt = connection.prepareStatement(find_sql);
        //为预编译参数赋值
        pstmt.setInt(1,id);
        //根据id查找
        ResultSet resultSet = pstmt.executeQuery();
        resultSet.next();
        School school = new School(id,
                resultSet.getString("description"),
                resultSet.getString("no"),
                resultSet.getString("remarks"));
        return school;
    }

    public static boolean update(School school,Connection connection) throws SQLException {


        //创建sql语句
        String updateSchool_sql = "update School set description=?,no=?,remarks=? where id=?";
        //在该链接上创建预编译语句对象
        PreparedStatement pstmt = connection.prepareStatement(updateSchool_sql);
        //为预编译参数赋值
        pstmt.setInt(4,school.getId());
        pstmt.setString(1,school.getDescription());
        pstmt.setString(2,school.getNo());
        pstmt.setString(3,school.getRemarks());
        //根据执行结果，具体判断所要返回的值
        if (pstmt.executeUpdate()==0){
            return false;
        }else
            return  true;
    }

    public static boolean add(School school,Connection connection) throws SQLException {


        //创建sql语句
        String addSchool_sql =
                "insert into School (description,no,remarks) values " +
                        "(?,?,?);";
        //在该链接上创建预编译语句对象
        PreparedStatement pstmt = connection.prepareStatement(addSchool_sql);
        //为预编译参数赋值

        pstmt.setString(1,school.getDescription());
        pstmt.setString(2,school.getNo());
        pstmt.setString(3,school.getRemarks());
        //获取执行结果
        int affectedRowNum = pstmt.executeUpdate();

        //根据结果，具体判断所要返回的值
        if (affectedRowNum == 1) {
            return true;
        } else
            return false;
    }

    public static boolean delete(int id,Connection connection) throws SQLException{

        //创建sql语句
        String deleteSchool_sql = "DELETE FROM School WHERE id=?";
        //在该连接上创建预编译语句对象
        PreparedStatement pstmt = connection.prepareStatement(deleteSchool_sql);
        //为预编译参数赋值
        pstmt.setInt(1,id);
        if (pstmt.executeLargeUpdate() == 1){
            return true;
        }else {
            return false;
        }
    }
    public static boolean delete(School school,Connection connection) throws SQLException {

        String deleteSchool_sql = "delete from School where no = ?";
        //在连接上创建预编译语句对象
        PreparedStatement pstmt = connection.prepareStatement(deleteSchool_sql);
        //为预编译参数赋值
        pstmt.setString(1,school.getNo());
        //根据id删除学院 并判断是否删除成功
        if(pstmt.executeUpdate() == 0){
            return false;
        }else {
            return true;
        }
    }


}


